/**
 * Created by jinxs on 14-7-8.
 * 依赖
 * ui.core.js
 */
(function ($) {
    var _cacheName = 'uiMask';
    /**
     * @class 遮罩组件
     * @constructor uiMask
     * @type {{alert: Function, confirm: Function, prompt: Function}}
     */
    $.uiMask = {
        /**
         * @summary 添加遮罩
         * @name uiMask#add
         * @function
         * @param options {Object} 组件的配置对象
         * @returns {jQuery} 组件对象
         */
        add: function (options) {
            options = options || {};
            options = $.extend({}, $.uiMask.defaults, options);
            return _add(options);
        },
        /**
         * @summary 显示遮罩
         * @name uiMask#show
         * @function
         * @param options {Object} 组件的配置对象
         * @returns {jQuery} 组件对象
         */
        show: function (options) {
            options = options || {};
            options = $.extend({}, $.uiMask.defaults, options);
            return _show(options);
        },
        /**
         * @summary 隐藏遮罩
         * @name uiMask#hide
         * @function
         * @param options {Object} 组件的配置对象
         * @returns {jQuery} 组件对象
         */
        hide: function (options) {
            options = options || {};
            options = $.extend({}, $.uiMask.defaults, options);
            var container = options.container;
            var contID = container.getElemID();
            var mask = $.getByParentID(contID, options.maskID);
            if (mask.length > 0) {
                var maskCache = mask.data(_cacheName);
                mask.hide();
                container.css({position: maskCache.parentPos, overflow: maskCache.parentOverflow});
            }
            container.zIndexHack(true, options.excludeSelector);
            return mask;
        },
        /**
         * @summary 移除遮罩
         * @name uiMask#remove
         * @function
         * @param options {Object} 组件的配置对象
         * @returns {jQuery} 组件对象
         */
        remove: function (options) {
            var mask = $.uiMask.hide(options);
            mask.remove();
            return mask;
        }
    };
    /**
     * 默认配置
     * @lends uiMask.prototype
     */
    $.uiMask.defaults = {
        /**
         * @type Object
         * @default body
         * @summary 组件父容器
         */
        container: $('body')
        /**
         * 在uiMask显示/隐藏时会对指定container下的OBJECT、IFRAME对象进行隐藏/显示
         * 有时需要排除对某些组件的这种控制
         * @type String
         * @default null
         * @summary 在container中要排除的对象选择器
         */
        , excludeSelector: null
        , maskID: 'mask'
        , zIndex: 888
    }
    var _add = function (options) {
        var container = options.container;
        container.setElemID();
        var elem = container[0];
        var contID = container.getElemID();
        var mask = $.getByParentID(contID, options.maskID);
        if (mask.length > 0) {
        } else {
            mask = $($.uiTemplate.mask)
                .appendTo(container).attr("id", $.getIDByParentID(contID, options.maskID));
            $(window).bind('resize', function () {
                if (mask.is(':visible')) {
                    mask.width(elem.scrollWidth).height(elem.scrollHeight);
                }
            });
        }
        return mask;
    };
    var _show = function (options) {
        var mask = _add(options);
        var container = options.container;
        var cache = {};
        cache.parentPos = container.css('position');
        cache.parentOverflow = container.css('overflow');
        container.zIndexHack(false, options.excludeSelector);
        mask.data(_cacheName, cache);
        container.css({position: 'absolute', overflow: 'hidden'});
        mask.css({zIndex: options.zIndex, width: container[0].scrollWidth, height: container[0].scrollHeight});
        mask.show();
        return mask;
    }
})(jQuery);
